<?php
class Question_Model extends CI_Model {

    var $strQuestion   = '';
    var $intType = 0;
    var $strType = '';
    var $table = 'tbquestions';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function listAll()
    {
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function getQuestionsByWizardID($idWizard)
    {
        $this->db->select(  'tbwizards.idWizard, tbquestions.idQuestion, tbanswers.idAnswer,
                            tbwizards.strTitle, tbquestions.strQuestion, tbanswers.strAnswer,
                            cattypesw.strType as wizard_type, cattypesq.strType as question_type,
                            cattypesw.idType as id_wizard_type, cattypesq.idType as id_question_type,
                            tbanswers.intPoints')
            ->from('tbwizards')
            ->join ('relwizards_questions', 'relwizards_questions.idWizard = tbwizards.idWizard')
            ->join ('tbquestions', 'tbquestions.idQuestion = relwizards_questions.idQuestion')
            ->join ('relquestions_answers', 'relquestions_answers.idQuestion = tbquestions.idQuestion')
            ->join ('tbanswers', 'tbanswers.idAnswer = relquestions_answers.idAnswer')
            ->join ('cattypes as cattypesw', 'cattypesw.idType = tbwizards.idType and cattypesw.strTable = "tbwizards"')
            ->join ('cattypes as cattypesq', 'cattypesq.idType = tbquestions.idType and cattypesq.strTable = "tbQuestions"')
            ->where("tbwizards.idWizard = {$idWizard}");

        $query = $this->db->get();
        return  $query->result();
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}
?>